<template>
  <div class="main-box">
    <div class="height-box">
      <div class="head-box" v-if="!isPhone">
        <PageHeader />
      </div>
      <div class="form-box" v-loading.fullscreen.lock="fullscreenLoading">
        <div class="login-title">登录</div>
        <div class="username-box">
          <div>账号</div>
          <input placeholder="请输入用户名" v-model="username" />
        </div>
        <div class="password-box">
          <div>密码</div>
          <input placeholder="请输入密码" v-model="password" type="password" />
        </div>
        <div class="check-box">
          <el-checkbox v-model="checked">记住密码</el-checkbox>
        </div>
        <div class="sub-btn-box" @click="login">登录</div>
      </div>
    </div>
  </div>
</template>

<script>
import PageHeader from '@/components/PageHeader.vue'
import {
  loginApi,
  EmployeeInfoGetApi,
  getAccessToken,
  getUser,
  getUserInfo
} from '@/api/index'

import * as dd from 'dingtalk-jsapi'

export default {
  computed: {
    dingName () {
      this.$router.replace('/tablePage')
      return this.$store.state.user.dingName
    }
  },
  data () {
    return {
      fullscreenLoading: false,
      username: '',
      password: '',
      checked: false,
      isPhone: false,

      // 钉钉信息
      code: '',
      accessToken: '',
      userId: {},
      userInfo: {},
      testCode: 'aaaaa'
    }
  },
  components: {
    PageHeader
  },
  created () {
    let info = navigator.userAgent
    this.isPhone = /mobile/i.test(info)
    this.$store.commit('user/setUserIsPhone', this.isPhone)
    this.getDingAPI()
  },
  methods: {
    // 获取员工信息
    async EmployeeInfoGet (name) {
      let data = {
        code: name
      }
      let res = await EmployeeInfoGetApi(data)
      if (res[0].empcode) {
        this.$store.commit('user/setUserData', res[0])
        this.fullscreenLoading = false
      } else {
        this.$message.error('登录失败！！！')
      }
    },

    async login () {
      this.fullscreenLoading = true
      if (this.username && this.password) {
        let data = {
          code: this.username,
          emppassword: this.password
        }
        let res = await loginApi(data)
        if (res.empcode) {
          this.fullscreenLoading = false
          this.$store.commit('user/setUserData', res)
          if (this.isPhone) {
            this.$router.replace('/MobileHomeView')
          } else {
            this.$router.replace('/tablePage')
          }
        } else {
          this.fullscreenLoading = false
          this.$message.error('登录失败！！！')
        }
      } else {
        this.$message.error('请输入用户名或密码！！！')
      }
    },

    // 链接钉钉Api,获取通行证
    async getDingAPI () {
      // this.fullscreenLoading = true
      console.log('引入钉钉---->', dd.env.platform)
      if (dd.env.platform !== 'notInDingTalk') {
        let _this = this
        let code
        dd.ready(function () {
          // dd.ready参数为回调函数，在环境准备就绪时触发，jsapi的调用需要保证在该回调函数触发后调用，否则无效。
          dd.runtime.permission.requestAuthCode({
            corpId: 'ding546527c2838da0e9',
            onSuccess: function (result) {
              // console.log('第一步：获取code',result)
              code = result.code
              _this.code = result.code
              _this.$store.commit('user/setCode', result.code)
            },
            onFail: function (err) {
              console.log('dd.onFail', err)
            }
          })
        })
        // 获取token
        let token = await getAccessToken({
          appKey: 'dinguorjkehhr1mhx7ui',
          appSecret:
            '9wtjy6eKbt5zP-Qky3qtvWZ-rJ5odNCCCrAeKkrwzWmDLGxNtNyx5OJM937LzIz9'
        })
        
        // console.log('第二步：获取token',token.accessToken)
        this.accessToken = token.accessToken
        console.log('accessToken--------->', this.accessToken)
        setTimeout(() => {
          this.onGetUser()
        }, 200)
      } else {
        this.$message.info('不在钉钉环境内，请使用账号密码登录！！！')
      }
    },

    // 获取用户id
    async onGetUser () {
      let result = await getUser(
        {
          access_token: this.accessToken
        },
        {
          code: this.code ? this.code : this.$store.state.user.dingCode
        }
      )
      if (result.errcode) {
        this.$message.error(`${result.errmsg}`)
        return
      } else {
        this.userInfo = result.result
        console.log('第三步：获取用户信息', this.userInfo)
        this.EmployeeInfoGet(this.userInfo.name)
      }

      // this.onGetUserInfo();
    },

    // 获取用户信息
    async onGetUserInfo () {
      let result = await getUserInfo(
        {
          access_token: this.accessToken
        },
        {
          userid: this.userId.userid,
          language: 'zh_CN'
        }
      )
      // console.log('第四',result)
      this.userInfo = result.result
      let payload = {
        userId: this.userId,
        userInfo: this.userInfo
      }
      this.$store.commit('user/setDingUserData', this.userId)
      this.$store.commit('user/setDingUserInfoData', this.userInfo)
      if (!this.$store.state.user.userIsPhone) {
        if (this.$route.fullPath !== '/tablePage') {
          this.$router.replace({
            name: 'tablePage',
            params: {
              id: this.userId.userid,
              name: this.userId.name
            }
          })
        }
      }
      // let whiteResult = await ContainsPhoneNumber({
      //   phoneNumber: this.userInfo.mobile,
      // });
      // this.isWhiteUser = whiteResult.result;
    }
  }
}
</script>

<style lang="scss" scoped>
.main-box {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url("../../assets/back.jpg");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-size: cover;
  background-attachment: fixed;
  .height-box {
    width: 99.5vw;

    /* margin-left: 67%; */
    height: 99.5vh;
    border: 1px solid rgba($color: #000, $alpha: 0%);
    border-radius: 20px;
    .form-box {
      /* height: 500px; */
      width: 35%;
      height: 70%;
      margin-top: 5%;
      margin-left: 57% !important;

      /* margin-left: 40px; */
      background: rgba($color: #fff, $alpha: 30%);

      /* border: 1px solid red; */
      border-radius: 20px;
      .login-title {
        /* border: 1px solid red; */

        /* height: 80px; */
        height: 18%;

        /* text-align: center; */

        /* padding-left: 30px; */
        padding-left: 6%;
        color: #353030;
        font-weight: bold;
        font-size: 40px;
        line-height: 80px;
      }
      .username-box,
      .password-box {
        /* border: 1px solid blue; */

        /* height: 90px; */
        height: 20%;
        margin-top: 20px;
        padding: 0 35px;
        > div {
          /* height: 40px; */
          height: 50%;
          font-size: 20px;
          line-height: 50%;
        }
        > input {
          width: 90%;
          height: 36px;
          border: 1px solid #faebeb;
          border-radius: 5px;
        }
        > input:focus {
          outline: 1px solid rgb(29 153 236);
        }
      }
      .check-box {
        margin-top: 10px;

        /* border: 1px solid blue; */
        padding-left: 30px;
      }
      .sub-btn-box {
        width: 70%;
        height: 40px;

        /* border: 1px solid blue; */
        margin-top: 40px;
        margin-left: 15%;
        color: #fff;
        font-size: 20px;
        line-height: 40px;
        text-align: center;
        background: #4299fc;
        border-radius: 10px;
      }
      .sub-btn-box:hover {
        background: rgba($color: #4299fc, $alpha: 70%);
        cursor: pointer;
      }
      .sub-btn-box:active {
        background: rgba($color: #232424, $alpha: 100%);
      }
    }
  }
}

@media screen and (width <= 768px) {
  .main-box {
    position: relative;
    width: 100%;

    /* height: 652px; */
    height: 100%;
    background-image: url("../../assets/back.jpg");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-size: cover;
    background-attachment: fixed;
    .height-box {
      width: 99.5vw;
      height: 99.5vh;

      /* border: 1px solid rgba($color: #ec1313, $alpha: 1); */

      /* border-radius: 20px; */
      margin-left: 1px;
      text-align: center;
      .form-box {
        width: 90%;
        height: 500px;
        margin-top: 40px;
        margin-left: 5% !important;
        margin-left: 40px;
        background: rgba($color: #fff, $alpha: 30%);

        /* border: 1px solid red; */
        border-radius: 20px;
        .login-title {
          /* border: 1px solid red; */
          height: 80px;

          /* text-align: center; */
          padding-left: 30px;
          color: #353030;
          font-weight: bold;
          font-size: 40px;
          line-height: 80px;
        }
        .username-box,
        .password-box {
          /* border: 1px solid blue; */
          height: 90px;
          margin-top: 20px;
          padding: 0 35px;
          > div {
            height: 40px;
            font-size: 20px;
            line-height: 40px;
          }
          > input {
            width: 90%;
            height: 36px;
            border: 1px solid #faebeb;
            border-radius: 5px;
          }
          > input:focus {
            outline: 1px solid rgb(29 153 236);
          }
        }
        .check-box {
          margin-top: 10px;

          /* border: 1px solid blue; */
          padding-left: 30px;
        }
        .sub-btn-box {
          width: 70%;
          height: 40px;

          /* border: 1px solid blue; */
          margin-top: 40px;
          margin-left: 15%;
          color: #fff;
          font-size: 20px;
          line-height: 40px;
          text-align: center;
          background: #4299fc;
          border-radius: 10px;
        }
        .sub-btn-box:hover {
          background: rgba($color: #4299fc, $alpha: 70%);
          cursor: pointer;
        }
        .sub-btn-box:active {
          background: rgba($color: #232424, $alpha: 100%);
        }
      }
    }
  }
}
</style>